package com.xts.weinfo.widget;

import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.helper.ItemTouchHelper;

import com.xts.weinfo.ui.adapter.RlvDailyNewsAdapter;
import com.xts.weinfo.ui.adapter.RlvInterestAdapter;

public class SimleCallback extends ItemTouchHelper.Callback {

    private ItemCallBack mCallBack;
    private boolean mDrag = true;
    private boolean mSwipe = true;

    public SimleCallback(ItemCallBack callBack){
        mCallBack = callBack;
    }

    /**
     * 返回可以滑动的方向,一般使用makeMovementFlags(int,int)或makeFlag(int, int)来构造我们的返回值
     * @param recyclerView
     * @param viewHolder
     * @return
     */
    @Override
    public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
        //允许拖拽的方向
        int drag = ItemTouchHelper.UP | ItemTouchHelper.DOWN;
        //允许侧滑的方向
        int swipe = ItemTouchHelper.LEFT;
        return makeMovementFlags(drag,swipe);
    }

    /**
     * 上下拖动item时回调,可以调用Adapter的notifyItemMoved方法来交换两个ViewHolder的位置，
     * 最后返回true，表示被拖动的ViewHolder已经移动到了目的位置,
     * @param recyclerView
     * @param viewHolder
     * @param target
     * @return
     */
    @Override
    public boolean onMove(RecyclerView recyclerView,
                          RecyclerView.ViewHolder viewHolder,
                          RecyclerView.ViewHolder target) {
        //上下拖拽的时候,我们需要将两个条目交互位置
        //viewHolder.getAdapterPosition()获取viewholder在适配器中的索引
        mCallBack.onItemMove(viewHolder.getAdapterPosition(),
                            target.getAdapterPosition());
        return true;
    }

    /**
     * 当用户左右滑动item时达到删除条件就会调用,一般为一半,条目继续滑动删除,否则弹回
     * @param viewHolder
     * @param direction
     */
    @Override
    public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
        mCallBack.onItemDelete(viewHolder.getAdapterPosition());
    }

    /**
     * 支持长按拖动,默认是true,上下
     * @return
     */
    @Override
    public boolean isLongPressDragEnabled() {
        return mDrag;
    }

    //是否允许上下拖拽
    public void setDragEnable(boolean dragEnable){
        this.mDrag = dragEnable;
    }

    //控制水平方向按着拖拽的
    @Override
    public boolean isItemViewSwipeEnabled() {
        return mSwipe;
    }

    public void setSwipeEnabled(boolean swipeEnabled){
        this.mSwipe = swipeEnabled;
    }
}
